﻿using System.Collections.Generic;
using System.Linq;
using Yun.Domain;
using Yun.Interface;
using Yun.Trade.Response;
using Yun.Util;

namespace Yun.Trade.Request
{
    /// <summary>
    /// 获取待创建的订单价格
    /// yun.order.precreate.amount
    /// </summary>
    public class GetOrderAmountRequest : ITopRequest<GetOrderAmountResponse>
    {
        /// <summary>
        /// 需要使用的积分，抵扣现金
        /// </summary>
        public List<CreditUseDetailJson> Integral { get; set; }


        /// <summary>
        /// 收货人地址
        /// </summary>
        public string Address { get; set; }


        private string _deliveryType;


        private string _promotionsActivityIdInShop;

        /// <summary>
        /// 所选择的商品信息
        /// itemid,quantity,skuid^itemid,quantity,skuid|itemid,quantity,skuid^itemid,quantity,skuid
        /// </summary>
        public IList<BuyItemBatch> Items { get; set; }


        /// <summary>
        /// 赠送的商品ID
        /// </summary>
        public List<List<TradeGiftJson>> GiftsJson { get; set; }

        /// <summary>
        /// 代金券ID
        /// </summary>
        public string CashCouponId { get; set; }


        /// <summary>
        /// 省ID
        /// </summary>
        public int? ProvinceId { get; set; }

        /// <summary>
        /// 市ID
        /// </summary>
        public int? CityId { get; set; }

        /// <summary>
        /// 区ID
        /// </summary>
        public int? AreaId { get; set; }

        /// <summary>
        /// 镇ID
        /// </summary>
        public int? TownId { get; set; }



        public string GetApiName()
        {
            return "yun.order.precreate.amount";
        }

        private string ConvertItems(IEnumerable<BuyItemBatch> items)
        {
            if (items == null) return "";

            var r = "";
            foreach (var item in items)
            {
                _deliveryType += item.DeliveryType + ",";
                _promotionsActivityIdInShop += item.PromotionsActivityIdInShop + ",";

                r = item.Items.Aggregate(r,
                    (current, i) =>
                        current +
                        string.Format("{0},{1},{2},{3}^", i.ItemId, i.Quantity, i.SkuId,
                            i.PromotionsActivityIdInItem));
                r = r.Trim('^') + "|";
            }

            return r;
        }

        public IDictionary<string, string> GetParameters()
        {
            var items = ConvertItems(Items);
            var parameters = new YunDictionary
            {
                {"items", items.Trim('|')},
                {"deliverytype", _deliveryType.Trim(',')},
                {"integral", Integral},
                {"address", Address},
                {"promotionsactivitiesidinshop", _promotionsActivityIdInShop.Trim(',')},
                {"giftsjson", GiftsJson},
                {"cashcouponid",CashCouponId},
                {"provinceid",ProvinceId},
                {"cityid", CityId},
                {"areaid", AreaId},
                {"townid", TownId}
            };
            return parameters;
        }

        public void Validate()
        {
            RequestValidator.ValidateRequired("items", Items);
        }
    }
}
